package dyyx.util;

import org.apache.commons.lang3.StringUtils;

import dyyx.dto.AsClient;
import dyyx.dto.MysqlClient;


public abstract class MysqlUtil {
	// private static final Random RAND = new Random();
	
	private static volatile MysqlClient client;

	
	public static synchronized void connect(String url,String userName,String userPwd)throws Exception{
		if(client!=null){
			throw new Exception("connected"); 
		}
		
		if(StringUtils.isBlank(url)){
			throw new Exception("url blank"); 
		}
		
		
		MysqlClient clienttmp = new MysqlClient( url, userName, userPwd);
		client = clienttmp;			
	}
	
	public static synchronized MysqlClient getClient(){
		if(client==null){
			throw new RuntimeException("not connected"); 
		}
		return client;
	}
	
	public static synchronized void disconnect()throws Exception{
		if(client==null){
			throw new Exception("not connected"); 
		}
		client.connection.close();
		client = null;
	}
	
	

}
